package testxml;

import java.util.Vector;
import javax.microedition.midlet.*;
import java.io.*;
import xmlutils.MapXmlData;
import org.kxml.parser.*;
import org.kxml.kdom.*;
import org.kxml.*;

/**
 * A simple app to demonstrate pull parsing using kXML.  It opens
 * a file from the resource directory /res then passes the InputStream
 * to a kXML parser.  The application will then ask the parser for the
 * next event.  If the start tag of an <address> element is 
 * encountered then control is passed to the parseAddress function
 * which will begin retrieving data and continue doing so until 
 * an end tag for </address> is encountered.  
 *
 * This demonstrates how easy it is to use a pull parser by 
 * fragmenting node parsing between functions.
 * 
 * @author Robert Cadena
 */
public class kXMLDemo_dom extends javax.microedition.midlet.MIDlet {

    /**
     * Name of resource file we'll use later
     */
    public static final String resfile_name = "/res/format.xml";

    /**
     * Creates an XML parser and parses a file in the local store into a
     * DOM tree.  Then we traverse the DOM and print out its contents.
     */
    public void beginParse() {
        XmlParser parser = null;
        Document doc = new Document();

        try {
            // Read in the resource
            InputStream in = this.getClass().getResourceAsStream(resfile_name);
            // Turn it into a reader
            InputStreamReader isr = new InputStreamReader(in);
            // initialize the parser with it.   
            parser = new XmlParser(isr);

            // Pass the parser to the document.  At this point the
            // entire resource is parsed and now resides in memory.
            doc.parse(parser);

            parser = null;
        } catch (IOException ioe) {
            // report error
            System.err.println("XML Parsing Error: " + ioe);
            ioe.printStackTrace();

            parser = null;
            doc = null;

            return;
        }
        try {

            MapXmlData mapData = new MapXmlData("http://www.alertmepro.com/images/format.xml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    // Now we get the root element which is "address_book"
    //Element root = doc.getRootElement();
    // Since the <address> tags are children of <address_book>
    // the we get the children of root and see which one is
    // called <address>.  We can't just say root.getElement("address")
    // because it will return an exception if more than one element
    // with that name exists
    //this.printTree(root);
//        int child_count = root.getChildCount();
//
//        for (int i = 0; i < child_count; i++) {
//            if (root.getType(i) != Xml.ELEMENT) {
//                continue;
//            }
//
//            Element kid = root.getElement(i);
//
//            // it's not an address? continue
//            if (!kid.getName().equals("address")) {
//
//                continue;
//            }
//
//            // it is an address, so let's print out the contents
//            System.err.println("--[ ADDRESS ]--------------------");
//
//            // traverse that portion of the tree looking for contents
//            // and printing them out
//            int address_item_count = kid.getChildCount();
//
//            for (int j = 0; j < address_item_count; j++) {
//                // if it's not an element tag then skip it
//                if (kid.getType(j) != Xml.ELEMENT) {
//                    continue;
//                }
//
//                Element item = kid.getElement(j);
//
//                System.err.println(item.getName() + ": " + item.getText(0));
//                item = null;
//            }
//
//
//            System.err.println("---------------------------------\n");
//
//            kid = null;
//        }


    // Now we get the root element which is "address_book"
    //Element root = doc.getRootElement();
    // Since the <address> tags are children of <address_book>
    // the we get the children of root and see which one is
    // called <address>.  We can't just say root.getElement("address")
    // because it will return an exception if more than one element
    // with that name exists

    //this.printTree(root);

//        int child_count = root.getChildCount();
//
//        for (int i = 0; i < child_count; i++) {
//            if (root.getType(i) != Xml.ELEMENT) {
//                continue;
//            }
//
//            Element kid = root.getElement(i);
//
//            // it's not an address? continue
//            if (!kid.getName().equals("address")) {
//
//                continue;
//            }
//
//            // it is an address, so let's print out the contents
//            System.err.println("--[ ADDRESS ]--------------------");
//
//            // traverse that portion of the tree looking for contents
//            // and printing them out
//            int address_item_count = kid.getChildCount();
//
//            for (int j = 0; j < address_item_count; j++) {
//                // if it's not an element tag then skip it
//                if (kid.getType(j) != Xml.ELEMENT) {
//                    continue;
//                }
//
//                Element item = kid.getElement(j);
//
//                System.err.println(item.getName() + ": " + item.getText(0));
//                item = null;
//            }
//
//
//            System.err.println("---------------------------------\n");
//
//            kid = null;
//        }
    }
    Element element = null;
    int elementType = -1;
    String text = "";

    private void printAttributes(Vector attributes) {
        if (attributes == null) {
            return;
        }
        for (int i = 0; i < attributes.size(); i++) {
            Attribute att = (Attribute) attributes.elementAt(i);
            System.out.print(" " + att.getName() + "=\"" + att.getValue() + "\"");
        }
    }

    private void printTree(Element treeRoot) {
        int elementsCount = treeRoot.getChildCount();
        for (int i = 0; i < elementsCount; i++) {
            elementType = treeRoot.getType(i);
            if (elementType != Xml.ELEMENT) {
                continue;
            }
            element = treeRoot.getElement(i);
            System.out.print("<" + element.getName());
            //  printAttributes(element.getAttributes());
            System.out.print(">");
            text = element.getText(0);
            if (text != null) {
                System.out.print(text.trim());
            } else {
                System.out.println("");
            }
            printTree(element);
        // System.out.println("</" + element.getName() + ">");

        }
    }

    /**
     * Starts the parser
     * 
     * @see javax.microedition.midlet.MIDlet#startApp()
     */
    protected void startApp() throws MIDletStateChangeException {
        beginParse();
    }

    /**
     * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
     */
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    }

    /**
     * @see javax.microedition.midlet.MIDlet#pauseApp()
     */
    protected void pauseApp() {
    }
}
